package com.bct.jxc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bct.jxc.common.Constants;
import com.bct.jxc.common.ErrorManager;
import com.bct.jxc.common.StringUtil;
import com.bct.jxc.model.Inport;
import com.bct.jxc.model.InportInfo;

public class InportDao implements BaseDao<Inport> {
	SqlManager manager = null;

	public InportDao() {
		super();
		manager = SqlManager.createInstance();
		manager.connectDB();// 获取数据库连接
	}
	/**
	 * 增加商品入库
	 */
	@Override
	public boolean add(Inport t) {
		// 编写sql语句
		String sql = "insert into t_import values(?,?,?,?,?,?,?,?,?,?)";
		// 封装参数
		Object[] params = {t.getId(),t.getAmount(),t.getPrice(), t.getCheckAccept(), t.getProviderId(),
				t.getInportTime(), t.getOperator(),t.getSponser(), t.getPayType(), t.getProductId()};
		return manager.executeUpdate(sql, params, Constants.PSTM_TYPE);
	}

	/**
	 * 动态生成编号
	 */
	@Override
	public String getId() {
		// sql语句，获得最新的商品编号
		String sql = "select MAX(id) as id from t_import";
		ResultSet rs = manager.executeQuery(sql, null, Constants.PSTM_TYPE);
		String id = "IP1001";
		try {
			if (rs != null && rs.next()) {
				String sid = rs.getString("id");// 获取前一次插入的id
				if (sid != null) {
					String s = sid.substring(2);
					id = "IP" + (Integer.parseInt(s) + 1);
				}
			}
		} catch (SQLException e) {
			ErrorManager.printError("InportDao getId()", e);
		}
		return id;
	}
	/**
	 * 查询入库信息
	 * @param i
	 * @return
	 */
	public List<InportInfo> findAll(InportInfo i) {
		String sql = null;
		Object params[] = null;
		List<InportInfo> list = null;
		if(i== null) {//查询全部
			sql = "select i.*,g.product_name,p.provider_name from t_import i,t_product g,t_provider p "
					+ "where i.product_id=g.id and i.provider_id=p.id";		
		}else {
			if(!StringUtil.isEmpty(i.getId())) {//根据入库单号查询
				sql="select i.*,g.product_name,p.provider_name from t_import i,t_product g,t_provider p " + 
						"  where i.product_id = g.id and i.provider_id = p.id and i.id =?";
				params = new Object[] {i.getId()};
			}else if(!StringUtil.isEmpty(i.getProviderName())) {//根据供应商名字查询
				sql ="select i.*,g.product_name,p.provider_name from t_import i,t_product g,t_provider p " + 
						"  where i.product_id = g.id and i.provider_id = p.id and p.provider_name like ?";
				params = new Object[] {"%"+i.getProviderName()+"%"};
			}else if(!StringUtil.isEmpty(i.getProductName())) {//根据商品名称查询
				sql ="select i.*,g.product_name,p.provider_name from t_import i,t_product g,t_provider p " + 
						"  where i.product_id = g.id and i.provider_id = p.id and g.product_name like ?";
				params = new Object[] {"%"+i.getProductName()+"%"};
			}else if(!StringUtil.isEmpty(i.getSponser())) {//根据经手人查询
				sql ="select i.*,g.product_name,p.provider_name from t_import i,t_product g,t_provider p " + 
						"  where i.product_id = g.id and i.provider_id = p.id and i.hand_person like ?";
				params = new Object[] {"%"+i.getSponser()+"%"};
			}else if(i.getStartTime()!=null && i.getEndTime()!=null) {//根据日期查询
				sql ="select i.*,g.product_name,p.provider_name from t_import i,t_product g,t_provider p " + 
						"  where i.product_id = g.id and i.provider_id = p.id and purchase_time between ? and ? ";
				params = new Object[] {i.getStartTime(),i.getEndTime()};
			}
		}
		ResultSet rs = manager.executeQuery(sql, params, Constants.PSTM_TYPE);
		if(rs != null) {
			list = new ArrayList<InportInfo>();
			try {
				while(rs.next()) {
					InportInfo info = new InportInfo();
					String id = rs.getString("id");
					int number = Integer.parseInt(rs.getString("amout"));
					float price = Float.parseFloat(rs.getString("price"));
					String checkAccept = rs.getString("check_accept");
					String providerId = rs.getString("provider_id");
					Date inportTime = rs.getDate("purchase_time");
					String operator = rs.getString("operator");
					String sponsor = rs.getString("hand_person");
					String paytype = rs.getString("method");
					String productId = rs.getString("product_id");
					String productName = rs.getString("product_name");
					String providerName = rs.getString("provider_name");
					info.setId(id);
					info.setAmount(number);
					info.setPrice(price);
					info.setCheckAccept(checkAccept);
					info.setProviderId(providerId);
					info.setInportTime(inportTime);
					info.setOperator(operator);
					info.setSponser(sponsor);
					info.setPayType(paytype);
					info.setProductId(productId);
					info.setProductName(productName);
					info.setProviderName(providerName);
					list.add(info);
				}
			} catch (SQLException e) {
				ErrorManager.printError("InportDao findAll()", e);
			}
		}
		return list;
	}
	@Override
	public List<Inport> finds(Inport t) {
		return null;
	}

	@Override
	public boolean update(Inport t) {
		return false;
	}

	@Override
	public boolean update(String id, Integer spare) {
		return false;
	}

}
